function validateCallback(form, callback) {
	var $form = $(form);

	if (!$form.valid()) {
		return false;
	}

	var _submitFn = function() {
		$.ajax({
			type : form.method || 'POST',
			url : $form.attr("action"),
			data : $form.serializeArray(),
			dataType : "text",
			cache : false,
			success : callback || sb.ajaxDone,
			error : sb.ajaxError
		});
	};

	var confirmMsg = $form.attr('title');
	if (confirmMsg) {
		layer.confirm(confirmMsg, {}, function(index) {
			_submitFn();
			layer.close(index);
		});
	} else {
		_submitFn();
	}
	return false;
}

function ajaxCallback(form) {
	var $form = $(form);

	var target = $form.attr('data-target');
	var $target = target ? $(target) : $(sb.setting.ajaxLoadTarget);

	var _submitFn = function() {
		$.ajax({
			type : form.method || 'POST',
			url : $form.attr("action"),
			data : $form.serializeArray(),
			dataType : "text",
			cache : false,
			success : function(data) {
				$target.html($(data)).initUI();
			},
			error : sb.ajaxError
		});
	};

	var confirmMsg = $form.attr('title');
	if (confirmMsg) {
		if (confirm(confirmMsg)) {
			_submitFn();
		}
	} else {
		_submitFn();
	}
	return false;
}

function _getPagerForm($parent, args) {
	var form = $("form", $parent).get(0);

	if (form) {
		if (args["pageNum"])
			form['pageNo'].value = args["pageNum"];
		if (args["numPerPage"])
			form['pageSize'].value = args["numPerPage"];
	}

	return form;
}

/**
 * 处理navTab中的分页和排序 targetType: navTab 或 dialog rel: 可选 用于局部刷新div id号 data: pagerForm参数 {pageNum:"n", numPerPage:"n", orderField:"xxx", orderDirection:""} callback: 加载完成回调函数
 */
function dwzPageBreak(options) {
	var op = $.extend({
		targetType : "navTab",
		rel : "",
		data : {
			pageNum : "",
			numPerPage : "",
			orderField : "",
			orderDirection : ""
		},
		callback : null
	}, options);

	if (op.rel) {
		var $box = op.rel;
		var form = _getPagerForm($box, op.data);
		if (form) {
			$(document).find('form').eq(0).submit();
			// $box.ajaxUrl({
			// url : $(form).attr("action"),
			// data : $(form).serializeArray()
			// });
		}
	}
}

function ajaxTodo(url, callback, target) {
	var $callback = callback || sb.ajaxDone;
	if (!$.isFunction($callback))
		$callback = eval('(' + callback + ')');

	var d = new Date();
	var param = url.indexOf('?') < 0 ? '?v=' + d.getTime() : '&v=' + d.getTime();
	$.ajax({
		type : 'POST',
		url : url + param,
		cache : false,
		success : $callback,
		error : function() {
			sb.error('加载出错');
		},
		dataTarget : target
	});
}

function ajaxLoad($target, url) {
	var d = new Date();
	var param = url.indexOf('?') < 0 ? '?v=' + d.getTime() : '&v=' + d.getTime();
	$.ajax({
		type : 'POST',
		url : url + param,
		cache : false,
		success : function(data) {
			try {
				var json = jQuery.isPlainObject(data) ? data : jQuery.parseJSON(data.trim());
				if (json.statusCode == '200') {
					sb.success(json.message);
				} else {
					sb.error(json.message);
				}
			} catch (e) {
				$target.html($(data)).initUI();
			}
		},
		error : function() {
			sb.error('加载出错');
		}
	});
}

$.fn.extend({
	ajaxTodo : function() {
		return this.each(function() {
			var $this = $(this);
			$this.click(function(event) {
				var $this = $(this);
				var url = $this.attr("href");

				var title = $this.attr("title");
				if (title) {
					layer.confirm(title, {
						shadeClose : true
					}, function() {
						ajaxTodo(url, $this.attr("callback"), $this.attr('data-target'));
						layer.closeAll('dialog');
					}, function() {

					});
				} else {
					ajaxTodo(url, $this.attr("callback"), $this.attr('data-target'));
				}
				event.preventDefault();
			});
		});
	},
	ajaxLoad : function() {
		return this.each(function() {
			var $this = $(this);
			$this.click(function(event) {
				var $this = $(this);
				var url = $this.attr("href");
				var target = $this.attr('data-target');

				var $target = target ? $(target) : $(sb.setting.ajaxLoadTarget);
				var title = $this.attr("title");
				if (title) {
					if (confirm(title)) {
						ajaxLoad($target, url);
					}
				} else {
					ajaxLoad($target, url);
				}
				event.preventDefault();
			});
		});
	},
	ajaxUrl : function(op) {
		var $this = $(this);

		$.ajax({
			type : op.type || 'GET',
			url : op.url,
			data : op.data,
			cache : false,
			success : function(response) {
				var json = sb.jsonEval(response);

				if (json[sb.keys.statusCode] == sb.statusCode.error) {
					if (json[sb.keys.message])
						sb.error(json[DWZ.keys.message]);
				} else {
					$this.html(response).initUI();
					if ($.isFunction(op.callback))
						op.callback(response);
				}
			},
			error : sb.ajaxError
		});
	},
});